package com.assa.jco.handler.simplehandler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.assa.jco.handler.FunctionHandler;
import com.assa.subscriber.EventSubscriber;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFieldIterator;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoRecordFieldIterator;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.server.JCoServerContext;

public class Z_TEST_JCO_Handler implements FunctionHandler {

	private static Log logger = LogFactory.getLog(Z_TEST_JCO_Handler.class);
	
	private String functionName;

	public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException {
		logger.info("----------------------------------------------------------------");
		logger.info("call              : " + function.getName());
		logger.info("ConnectionId      : " + serverCtx.getConnectionID());
		logger.info("SessionId         : " + serverCtx.getSessionID());
		logger.info("TID               : " + serverCtx.getTID());
		logger.info("repository name   : " + serverCtx.getRepository().getName());
		logger.info("is in transaction : " + serverCtx.isInTransaction());
		logger.info("is stateful       : " + serverCtx.isStatefulSession());
		logger.info("----------------------------------------------------------------");
		logger.info("gwhost: " + serverCtx.getServer().getGatewayHost());
		logger.info("gwserv: " + serverCtx.getServer().getGatewayService());
		logger.info("progid: " + serverCtx.getServer().getProgramID());
		logger.info("----------------------------------------------------------------");
		logger.info("attributes  : ");
		logger.info(serverCtx.getConnectionAttributes().toString());
		logger.info("----------------------------------------------------------------");
		logger.info("CPIC conversation ID: " + serverCtx.getConnectionAttributes().getCPICConversationID());
		logger.info("----------------------------------------------------------------");
		logger.info("Exports: ");

		JCoFieldIterator iterator = function.getImportParameterList().getFieldIterator();

		while (iterator.hasNextField()) {
			JCoField field = iterator.nextField();
			logger.info("Field :" + field.getName() + " tipo: " + field.getTypeAsString());
			logger.info("Valor: " +  field.getString());
			
		}
		


		function.getExportParameterList().setValue("RETORNO_XI", "OK");

	}

	public String getFunctionName() {
		return this.functionName;
	}

	public void setFunctionName(String name) {
		this.functionName = name;
	}

	public void setEventSubscriber(EventSubscriber subscriber) {
		// TODO Auto-generated method stub
		
	}

}
